package com.imooc.controller;

import com.imooc.pojo.SysParams;
import com.imooc.result.GraceJSONResult;
import com.imooc.result.ResponseStatusEnum;
import com.imooc.service.SysParamsService;
import com.imooc.vo.SysParamsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liansen
 * @create 05-19-14:30
 */
@RestController
@RequestMapping("/sys")
public class SysParamController {

    @Autowired
    private SysParamsService sysParamsService;

    /**
     * 设置简历刷新的最大次数
     * @param maxCounts
     * @param version
     * @return
     */
    @PostMapping("/modifyMaxResumeRefreshCounts")
    public GraceJSONResult modifyMaxResumeRefreshCounts(Integer maxCounts, Integer version){
        if(maxCounts == null || maxCounts < 1){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.SYSTEM_PARAMS_SETTINGS_ERROR);
        }
        sysParamsService.modifyMaxResumeRefreshCounts(maxCounts, version);
        //TODO zk的乐观锁机制
        return GraceJSONResult.ok(0);
    }

    /**
     * 查询简历刷新的最大次数
     */
    @PostMapping("/params")
    public GraceJSONResult getMaxCount(){
        SysParams sysParams = sysParamsService.getById(1001);
        SysParamsVO sysParamsVO = new SysParamsVO();
        BeanUtils.copyProperties(sysParams, sysParamsVO);
        sysParamsVO.setVersion(0);
        return GraceJSONResult.ok(sysParamsVO);
    }
}
